﻿using h1;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Configuration;
using Sunisoft.IrisSkin;

namespace h1
{
    public partial class Form_list : Form
    {
        Form1 f1;
        string select;
        string id;
        public Form_list()
        {
            InitializeComponent();
        }
        public Form_list(Form1 f, string select)
        {
            InitializeComponent();
            this.select = select;
            f1 = f;
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Form_user form_User = new Form_user(f1, this, select);
            form_User.ShowDialog();
        }
        private void loadUser()
        {
            DataSet ds_users = MySqlHelper.getDataSet(MySqlHelper.Conn, "SELECT * FROM users");
            id = ds_users.Tables[0].Select(select)[0]["userID"].ToString();
            toolStripStatusLabel1.Text = "用户名:" + ds_users.Tables[0].Select(select)[0]["userName"].ToString() + "  用户ID:" + id;
            string skin = ds_users.Tables[0].Select(select)[0]["userSkin"].ToString();
            if (skin == "" || skin == "默认")
            {
                Form1.skinEngine.Active = false;
            }
            else
            {
                Form1.skinEngine.SkinFile = System.Environment.CurrentDirectory + $"\\Skins\\{skin}.ssk";
                Form1.skinEngine.Active = true;
            }
        }
        private void loadArr()
        {
            DataSet ds_arr = MySqlHelper.getDataSet(MySqlHelper.Conn, $"SELECT * FROM arrangements WHERE userID = '{id}'");
            dataGridView1.DataSource = ds_arr.Tables[0];
            DataSet ds_lists = MySqlHelper.getDataSet(MySqlHelper.Conn, $"SELECT * FROM lists WHERE userID = '{id}'");
            if (ds_lists.Tables[0].Rows.Count < 1)
            {
                DataRow newRow = ds_lists.Tables[0].NewRow();
                newRow["userID"] = id;
                newRow["listID"] = id + "00001";
                newRow["listName"] = "默认";
                ds_lists.Tables[0].Rows.Add(newRow);
                MySqlHelper.updateDataSet(MySqlHelper.Conn, $"SELECT * FROM lists WHERE userID = '{id}'", ds_lists);
            }
        }
        private void Form_list_Load(object sender, EventArgs e)
        {
            loadUser();
            loadArr();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable ds = (DataTable)dataGridView1.DataSource;
            MySqlHelper.updateDataSet(MySqlHelper.Conn, $"SELECT * FROM arrangements WHERE userID = '{id}'", ds);
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Form_schedule form_Schedule = new Form_schedule(id);
            form_Schedule.dateTimePicker1.Value = DateTime.Now;
            form_Schedule.dateTimePicker2.Value = DateTime.Now.AddHours(1);
            form_Schedule.ShowDialog();
            loadArr();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Form_createList form_CreateList = new Form_createList(id);
            form_CreateList.ShowDialog();
            loadArr();
        }
    }
}

